package com.dfch.auto.appium.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;

public enum AndroidCMDEnum {

    input("控件||输入"),
    click("控件||点击"),
    clear("控件||清空"),

    press("操作||按下"),
    moveTo("操作||滑动"),
    tap("操作||触碰"),
    wait("操作||等待"),
    longPress("操作||长按"),
    cancel("操作||取消"),
    perform("操作||执行"),
    release("操作||释放"),

    swipeUp("滑动||上"),
    swipeDown("滑动||下"),
    swipeLeft("滑动||左"),
    swipeRight("滑动||右"),

    sleep("线程||等待");

    private static final Logger LOGGER = LoggerFactory.getLogger(AndroidCMDEnum.class);

    public static final Map<String, Method> methods = new HashMap();

    static {
        try {
            Method clear = MobileElement.class.getMethod("clear");
            Method send = MobileElement.class.getMethod("sendKeys", CharSequence[].class);
            Method click = MobileElement.class.getMethod("click");

            methods.put("click", click);
            methods.put("clear", clear);
            methods.put("input", send);

            Method press = TouchAction.class.getMethod("press", int.class, int.class);
            Method moveTo = TouchAction.class.getMethod("moveTo", int.class, int.class);
            Method tap = TouchAction.class.getMethod("tap", int.class, int.class);
            Method wait = TouchAction.class.getMethod("wait", long.class);
            Method longPress = TouchAction.class.getMethod("longPress", int.class, int.class);
            Method cancel = TouchAction.class.getMethod("cancel");
            Method perform = TouchAction.class.getMethod("perform");
            Method release = TouchAction.class.getMethod("release");

            methods.put("press", press);
            methods.put("moveTo", moveTo);
            methods.put("tap", tap);
            methods.put("wait", wait);

            methods.put("longPress", longPress);
            methods.put("cancel", cancel);
            methods.put("perform", perform);
            methods.put("release", release);

            Method swipeUp = AndroidCMDUtil.class.getDeclaredMethod("swipeUp", AndroidDriver.class);
            Method swipeDown = AndroidCMDUtil.class.getMethod("swipeDown", AndroidDriver.class);
            Method swipeLeft = AndroidCMDUtil.class.getMethod("swipeLeft", AndroidDriver.class);
            Method swipeRight = AndroidCMDUtil.class.getMethod("swipeRight", AndroidDriver.class);

            methods.put("swipeUp", swipeUp);
            methods.put("swipeDown", swipeDown);
            methods.put("swipeLeft", swipeLeft);
            methods.put("swipeRight", swipeRight);
            methods.put("sleep", wait);

        } catch (Exception ex) {
            LOGGER.error("prepare auto sdk methods failed:", ex);
        }
    }


    AndroidCMDEnum(String desc) {
        this.desc = desc;
    }

    private String desc;

    public String getDesc() {
        return desc;
    }


    public Method getTargetMethod() {
        return methods.get(this.toString());
    }


    public static void main(String[] args) {
        Thread.class.getDeclaredMethods();
        Method method = AndroidCMDEnum.sleep.getTargetMethod();
        try {
            method.invoke(Thread.class, 1000L);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
